package com.lz.football.module.compete
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ClubConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.events.MainEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.compete.CompeteDataManager;
	import com.liangzhong.model.compete.vo.ChallengeCostVo;
	import com.liangzhong.model.friend.vo.FriendInfoVo;
	import com.liangzhong.model.ladderMatch.vo.MatchTimeVo;
	import com.lz.football.module.playerCenter.ListItem;
	
	import common.BaseView.BaseView;
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.ImageContain;
	import lz.controls.LButton;
	
	/**
	 *个人挑战 
	 * @author Administrator
	 * 
	 */	
	public class SingleChaglleView extends BaseView
	{
		/**
		 *名称  
		 */
		private var playerName:TextField;
		/**
		 *国家 
		 */
		private var country:TextField;
		/**
		 *国家图标  
		 */
		private var nationIcon:MovieClip;
		/**
		 *等级  
		 */
		private var Lv:TextField;
		/**
		 *综合能力 
		 */
		private var ability:TextField;
//		/**
//		 *选择阵容mc 
//		 */		
//		private var selectLineUpMc :MovieClip;
//		/**
//		 *阵容选择下拉 
//		 */		
//		private var lineUpCombMc	:MovieClip;
		/**
		 *挑战按钮 
		 */
		private var changeBtn:LButton;
		private var friendVo:FriendInfoVo;
		/**
		 * 
		 */
		private var teamIcon:MovieClip;
		private var playerContain:MovieClip;
		/**
		 *挑战图标 
		 */
		private var rIcon:MovieClip;
		private var _changellcost:ChallengeCostVo;
		/**
		 *是否可以比赛 
		 */
		private var _isSureMatch:Boolean;
		public function SingleChaglleView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.modul.compete.singleChallengPanel");
			_windowBg = symbolSkin.changellBg;
			this.addChild(symbolSkin);
			symbolSkin.cacheAsBitmap = true;
			isDrag = true;
			isCloseBtn = true;
			_viewHeight = symbolSkin.height;
			_viewWidth  = symbolSkin.width;
			teamIcon = symbolSkin.teamIcon;
			nationIcon = symbolSkin.nationIcon;
			onResizeHandler();
			playerContain = symbolSkin.playerContain;
//			lineUpCombMc = Reflection.createInstance("com.football.compete.lineUpCombSkin");
			playerName = symbolSkin.playerName;
			country = symbolSkin.country;
			Lv = symbolSkin.Lv;
			ability = symbolSkin.ability;
			rIcon = symbolSkin.rIcon;
			symbolSkin.buff.buff.gotoAndStop(2);
//			selectLineUpMc = symbolSkin.selectLineUpMc;
//			selectLineUpMc.buttonMode = true;
//			selectLineUpMc.txtName.mouseEnabled = false;
			changeBtn = new LButton(symbolSkin.changeBtn);
			changeBtn.enable = true;
			symbolSkin.changeBtn.cost.mouseEnabled = false;
			moveClose(_viewWidth - 30,5);
			addEvtListener();
		}
		
		/**
		 *设置数据 
		 * 
		 */
		public function setData($vo:FriendInfoVo):void
		{
			changeBtn.addEventListener(MouseEvent.CLICK,onchangeHandler);
			while(playerContain.numChildren)
			{
				(playerContain.removeChildAt(0) as ListItem).destroyView();
			}
			friendVo = $vo;
			playerName.text = friendVo.uname + "";
			country.text = "国家："+ClubConst.countryToString(friendVo.country) + "";
			nationIcon.gotoAndStop(friendVo.country);
			Lv.text = "Lv."+friendVo.level + "";
			ability.text = friendVo.ability + "";
			
			DisplayObjectUtil.removeAllChildren(teamIcon);
//			var lo:MovieClip = Reflection.createInstance(AppData.getInstance().clubDatamanager.getTeamIconVo(friendVo.logo).iconurl);
//			lo.scaleX = lo.scaleY = 0.4;
//			lo.x = -lo.width >> 1;
//			lo.y = -lo.height >> 1;
//			teamIcon.addChild(lo);
			
			
			if(AppData.getInstance().clubDatamanager.getTeamIconVo($vo.logo) != null)
			{
				var lo:MovieClip = Reflection.createInstance(AppData.getInstance().clubDatamanager.getTeamIconVo($vo.logo).iconurl);
				if(lo != null)
				{
					
						lo.scaleX = lo.scaleY = 0.4;
					
					lo.x = -lo.width >> 1;
					lo.y = -lo.height >> 1;
					teamIcon.addChild(lo);
					
				}
			}else
			{
				var iC:ImageContain = new ImageContain();
				iC.setSize(50,50);
				iC.url = GlobalDef.Web_Res_Url + "clubicon/50-50/" + $vo.logo + ".png";
				iC.x = -iC.width >> 1;
				iC.y = -iC.height >> 1;
				teamIcon.addChild(iC);
			}
			
			
			
			var arr:Array = friendVo.highPlayerList;
			var len:int;
			var i:int;
			var item:ListItem;
			len = arr.length;
			for(i = 0;i < len;i++)
			{
				item = new ListItem();
				item.initUI();
				item.removeBar();
				item.isShowTips = true;
				item.data = arr[i];
				item.x = (item.width + 2)* i;
				playerContain.addChild(item);
			}
			setChanllange();
			var sort:int = AppData.getInstance().competeDataManager.getopponentRankSort(friendVo.friendid);
			symbolSkin.rank.text = sort+ "";
			sort = AppData.getInstance().competeDataManager.competeInfoVo.rivalnationprerank;
			symbolSkin.buff.buff.gotoAndStop(sort);
			var str:String = CompeteDataManager.getcompeteBuffer(sort);
			DisplayMouseEventManager.instance.addTipsDisplay(symbolSkin.buff,str);
			
			checkTimeIsSureMatch();
		}
		
		/**
		 * 检测时间 是否可以比赛
		 * 
		 */
		private function checkTimeIsSureMatch():void
		{
			var d :Date = AppData.getInstance().curDate;
			var arr:Array = AppData.getInstance().competeDataManager.matchTimeList;
			var len:int = arr.length;
			var vo:MatchTimeVo;
			_isSureMatch = false;
			for(var i:int = 0;i < len;i++)
			{
				vo = arr[i];
				if(vo.starTime<=d.getHours()&& d.getHours() < vo.endTime )
				{
					_isSureMatch = true;
					break;
				}
			}
		}
		
		private function setChanllange():void
		{
			_changellcost = AppData.getInstance().competeDataManager.selectChallengCost;
			symbolSkin.rival.text = _changellcost.win + "";
			symbolSkin.changeBtn.cost.text = _changellcost.cost + "";
			symbolSkin.changeBtn.cost.mouseEnabled = false;
		}
		private function isChangell($type:int):void
		{
			if($type == Alert.YES)
				AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.CHANGELL,{id:friendVo.friendid,formation:lineUpIndex}));
		}
		override public function addEvtListener():void
		{
//			selectLineUpMc.addEventListener(MouseEvent.CLICK,lineUpHandler);
//			lineUpCombMc.addEventListener(MouseEvent.CLICK,lineUpClickHandler);
			changeBtn.addEventListener(MouseEvent.CLICK,onchangeHandler);
			DisplayMouseEventManager.instance.addTipsDisplay(rIcon,"挑战点可以增加国家排名和自己在国内的排名！\n赢取的对手排名越靠前，挑战点的收获越丰厚！");
		}
		
		protected function onchangeHandler(event:Event):void
		{
//			symbolSkin.changeBtn.cost.text = _changellcost.cost + "";
//			symbolSkin.changeBtn.cost.mouseEnabled = false;
			// TODO Auto-generated method stub
			if(!_isSureMatch)
			{
				Alert.showErrorTip("国家挑战赛开放时间为10-13点和19-22点！");
				return;
			}
			
			
			
			if(_changellcost.cost > AppData.getInstance().clubDatamanager.clubInfo.coin)
			{
				Alert.showErrorTip("欧元不足，不可以挑战");
				return;
			}
			if(AppData.getInstance().playerCenterDataManager.fieldPlayerinjuredNum)
			{
				Alert.show("您的上场球员已经有5名受伤，球员们联合起来罢赛了！" +
					"他们抗议如此赤果果的压榨行为并且上报国际足联，老板，赶快去给他们疗伤吧~",
					"",Alert.NO,GlobalDef.AlertContainer,openPlayerCenter);
				return;
			}
			
			if(AppData.getInstance().playerCenterDataManager.fieldisinjured)
			{
				Alert.show("您当前的队伍中有球员受伤，会大大影响球队的实力！\n您可以在球员中心治疗受伤的球员！","",Alert.NO,GlobalDef.AlertContainer,sureMatch);
			}else
			{
				sureMatch(Alert.YES);
			}
			
			
//			Alert.show("是否挑战" +friendVo.uname,"",Alert.NO,GlobalDef.AlertContainer,isChangell);
		}
		private function openPlayerCenter(type:int):void
		{
			if(type == Alert.YES)
			{
				AppEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.OPEN_PLAYERCENTER));
			}
		}
		/**
		 *确认比赛 
		 * 
		 */
		private function sureMatch(type:int):void
		{
			if(type == Alert.YES)
			{
				changeBtn.removeEventListener(MouseEvent.CLICK,onchangeHandler);
				AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.CHANGELL,{id:friendVo.friendid,formation:lineUpIndex}));
			}
		}
		/**
		 *选择的阵容索引 
		 */		
		private var lineUpIndex	:int = 1;
		
		/**
		 *阵容选择 
		 * @param e
		 */		
		private function lineUpHandler(e :MouseEvent):void
		{
//			if(lineUpCombMc.parent)
//			{
//				lineUpCombMc.parent.removeChild(lineUpCombMc);
//			}
//			else
//			{
//				symbolSkin.addChild(lineUpCombMc);
//				lineUpCombMc.x = selectLineUpMc.x;
//				lineUpCombMc.y = selectLineUpMc.y + selectLineUpMc.height +3;
//			}
		}
		/**
		 *阵型点击 
		 * @param e
		 * 
		 */		
		private function lineUpClickHandler(e :MouseEvent):void
		{
//			if(e.target.name.indexOf("item_") != -1)
//			{
//				lineUpIndex = e.target.name.split("_")[1];
//				if(lineUpIndex == 1)
//				{
//					selectLineUpMc.txtName.text = "首发阵容";
//				}
//				else
//				{
//					selectLineUpMc.txtName.text = "第二阵容";
//				}
//				if(lineUpCombMc && lineUpCombMc.parent)
//				{
//					lineUpCombMc.parent.removeChild(lineUpCombMc);
//				}
//			}
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
//			selectLineUpMc.removeEventListener(MouseEvent.CLICK,lineUpHandler);
//			lineUpCombMc.removeEventListener(MouseEvent.CLICK,lineUpClickHandler);
			changeBtn.removeEventListener(MouseEvent.CLICK,onchangeHandler);
			DisplayMouseEventManager.instance.removeTipsDisplay(rIcon);
			DisplayMouseEventManager.instance.removeTipsDisplay(symbolSkin.buff);
		}
		override public function destroyView():void
		{
			
			removeEvtListener();
			if(this.parent)this.parent.removeChild(this);
			DisplayObjectUtil.removeAllChildren(teamIcon);
			while(playerContain.numChildren)
			{
				(playerContain.removeChildAt(0) as ListItem).destroyView();
			}
			super.destroyView();
			
		}
	}
}